"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具关闭dssserver进程
Create At   : 2023/4/19
Owner       : opentestcase019
Description :
    1.关闭dssserver进程，不指定-U UDS:socket_domain参数
    2.关闭dssserver进程，指定-U UDS:socket_domain参数为正常值
    3.关闭dssserver进程，指定错误的-U UDS:socket_domain参数
    4.关闭dssserver进程，指定权限不足的socket_domain
    5.在备机点执行关闭dssserver进程
    6.在dss关闭的情况下执行(连续执行多次)
    7.在集群停止的情况下执行
    8.DSS_HOME路径权限不足时，关闭dssserver进程，不指定-U UDS:socket_domain参数
Expect      :
    1.关闭dssserver进程成功，dss状态为OffLine
    2.关闭dssserver进程成功，dss状态为OffLine
    3.合理报错Failed to get uds connection.
    4.合理报错Failed to get uds connection.
    5.关闭dssserver进程成功，dss状态为OffLine
    6.合理报错
    7.合理报错
    8.合理报错
History     :
"""
import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.standby_node = Node('Standby1DbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:关闭dssserver进程，不指定-U UDS:socket_domain参数;" \
                "expect:关闭dssserver进程成功，dss状态为OffLine-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd stopdss;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Succeed to stop server.', execute_msg, "执行失败" + step1)
        for i in range(30):
            status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
            self.logger.info(status)
            if "OffLine" in status:
                break
        time.sleep(30)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step1)

        step2 = "-----step2:关闭dssserver进程，指定-U UDS:socket_domain参数为正常值;" \
                "expect:关闭dssserver进程成功，dss状态为OffLine-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd stopdss -U {macro.UDS_PATH};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Succeed to stop server.', execute_msg, "执行失败" + step2)
        for i in range(30):
            status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
            self.logger.info(status)
            if "OffLine" in status:
                break
        time.sleep(30)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step2)

        step3 = "-----step3:关闭dssserver进程，指定错误的-U UDS:socket_domain参数;" \
                "expect:合理报错Failed to get uds connection.-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd stopdss -U {macro.UDS_PATH}.cfg;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Failed to get uds connection.',
                      execute_msg, "执行失败" + step3)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertNotIn("OffLine", status, "执行失败" + step3)

        step4 = "-----step4:关闭dssserver进程，指定权限不足的socket_domain;expect:" \
                "合理报错Failed to get uds connection.-----"
        self.logger.info(step4)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"chmod 000 {macro.UDS_PATH[4::]};" \
            f"dsscmd stopdss -U {macro.UDS_PATH};" \
            f"chmod 600 {macro.UDS_PATH[4::]}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step4)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertNotIn("OffLine", status, "执行失败" + step4)

        step5 = "-----step5:在备机点执行关闭dssserver进程;" \
                "expect:关闭dssserver进程成功，dss状态为OffLine-----"
        self.logger.info(step5)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd stopdss;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('Succeed to stop server.', execute_msg, "执行失败" + step5)
        for i in range(30):
            status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
            self.logger.info(status)
            if "OffLine" in status:
                break
        time.sleep(30)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step5)

        step6 = "-----step6:在dss关闭的情况下执行(连续执行多次);expect:合理报错-----"
        self.logger.info(step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd stopdss;sleep 2;" \
            f"dsscmd stopdss;" \
            f"dsscmd stopdss -U {macro.UDS_PATH};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('Succeed to stop server.', execute_msg, "执行失败" + step6)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step6)

        step7 = "-----step7:在集群停止的情况下执行;expect:合理报错-----"
        self.logger.info(step7)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step7)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd stopdss ;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Failed to get uds connection.',
                      execute_msg, "执行失败" + step7)
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败')

        step8 = "-----step8:DSS_HOME路径权限不足时，关闭dssserver进程，" \
                "不指定-U UDS:socket_domain参数;expect:合理报错-----"
        self.logger.info(step8)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"chmod 000 {macro.UDS_PATH[4::]};" \
            f"dsscmd stopdss;" \
            f"chmod 600 {macro.UDS_PATH[4::]}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step8)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertNotIn("OffLine", status, "执行失败" + step8)

    def tearDown(self):
        self.logger.info("-----无需清理环境-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
